package com.yuyang.wifi.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.yuyang.wifi.MyApplication;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;

import java.io.File;

import cz.msebera.android.httpclient.Header;


/**
 * created by mej
 * on 2021/4/16
 */
public class ApkDownLoadUtils {

    private static String mDictionary, mLocalPath, mFileUrl;
    private static Context mContext;
    private static RequestHandle mDownloader;

    public static void downLoadUrl(Context context, String downLoadUrl) {
        mContext = context;
        mFileUrl = downLoadUrl;
        if (TextUtils.isEmpty(mLocalPath)) {
            down();
        } else {
            installApk(mLocalPath);
        }
    }

    private static void down() {
        final String fileUrl = mFileUrl;
        mDictionary = PathUtils.getApkPath(mContext);
        mDownloader = HttpUtil.downH5(fileUrl, new FileAsyncHttpResponseHandler(mContext) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, File file) {
                LogUtils.e("onSuccess", file.getPath());
                String movieFileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
                String localPath = mDictionary + movieFileName;
                LogUtils.e("localPath", localPath);
                try {
                    FileUtil.copyFile(new File(file.getPath()), new File(localPath));
                    file.delete();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mLocalPath = localPath;
                installApk(mLocalPath);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
                LogUtils.e("onFailure", statusCode + "");
                ToastUtils.showShortToast(mContext, "下载失败，请稍后再试");

                mLocalPath = "";

            }

            @Override
            public void onProgress(final long bytesWritten, final long totalSize) {
                super.onProgress(bytesWritten, totalSize);
                int progress = (int) (bytesWritten * 100 / totalSize);
                // updateProgress(progress);
            }
        });
    }

    public static void installApk(String path) {
        MyApplication.logEvent(mContext, "用户强制升级");
        File apkfile = new File(path);
        if (!apkfile.exists() && apkfile.length() > 10) {
            return;
        }

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        Uri uri = DCommManager.makeUri(MyApplication.getInstance(), apkfile);
        i.setDataAndType(uri, "application/vnd.android.package-archive");
        LogUtils.e("path", path);
        LogUtils.e("prividerpath", uri.getPath());
        mContext.startActivity(i);
    }

}
